package com.spark.tags;

import java.util.Map;

import com.spark.core.SparkContext;
import com.spark.core.XMap;

public class Label extends ViewComponent {
	
	private static final String DEFAULT_CLASS = "s_label";
	private static final String DEFAULT_TEMPLATES = "label.ftl";
	public static final String PROPERTITY_RENDERER = "renderer";
	
	protected int getDefaultWidth(){
		return 152;
	}
	
	protected String getDefaultClass(XMap view, SparkContext context) {
		String className = super.getDefaultClass(view, context);
		StringBuilder sb = new StringBuilder();
		sb.append(className);
		if (!"".equals(className.trim()))
			sb.append(" ");
		sb.append(DEFAULT_CLASS);
		return sb.toString();
	}
	
	public String getTemplate() {
		return DEFAULT_TEMPLATES;
	}
	
	public void init(XMap view, SparkContext context) throws Exception {
		super.init(view, context);
		String renderer = view.getString(PROPERTITY_RENDERER);
		if(renderer!=null)
		context.addJsonConfig(PROPERTITY_RENDERER,renderer);
	}
	
	@SuppressWarnings("unchecked")
	protected String build(SparkContext context){
		Map map = context.getMap();
		StringBuilder sb = new StringBuilder();
		sb.append("<DIV class=\"").append(get(map,"className")).append("\" style=\"width:").append(get(map,"width")).append("px;").append(get(map,"style")).append("\" id=\"").append(get(map,"id")).append("\"></DIV>\r");
		sb.append("<script>new Spark.Label(").append(get(map,"config")).append(");</script>\r");
		return sb.toString();
	}
}
